package away3d.entities ;


import away3d.arcane ;
import away3d.cameras.lenses.PerspectiveLens ;
import away3d.containers.ObjectContainer3D ;

import away3d.core.managers.Texture3DProxy ;

import flash.display.BitmapData ;
import flash.geom.Matrix3D ;
import flash.geom.Vector3D ;



// tho this is technically not an entity, to the user it functions similarly, so it's in the same package
class TextureProjector extends ObjectContainer3D
{
    
    
    private var _lens                       : PerspectiveLens ;
    private var _viewProjectionInvalid      : Bool ;
    private var _viewProjection             : Matrix3D ;
    private var _texture                    : Texture3DProxy ;
    
    
    
    public function TextureProjector( bitmapData : BitmapData )
    {
        
        _viewProjectionInvalid      = true ;
        _viewProjection             = new Matrix3D() ;
        _lens                       = new PerspectiveLens() ;
        _lens.onInvalidateMatrix    = onInvalidateLensMatrix ;
        _texture                    = new Texture3DProxy() ;
        _texture.bitmapData         = bitmapData ;
        _lens.aspectRatio           = bitmapData.width/bitmapData.height ;
//            lookAt( new Vector3D( 0, -1000, 0 ) ) ;
        rotationX                   = -90 ;
        
    }
    
    
    public var aspectRation( get_aspectRatio, set_aspectRatio ) : Float ;
    
    
    private function get_aspectRatio() : Float
    {
        
        return _lens.aspectRatio ;
        
    }
    
    
    private function set_aspectRatio( value : Float )
    {
        
        _lens.aspectRatio = value ;
        
    }
    
    
    public var fieldOfView( get_fieldOfView, set_fieldOfView ) : Float ;
    
    
    private function get_fieldOfView() : Float
    {
        
        return _lens.fieldOfView ;
        
    }
    
    
    private function set_fieldOfView( value : Float )
    {
        
        _lens.fieldOfView = value ;
        
    }
    
    
    public var bitmapData( get_bitmapData, set_bitmapData ) : BitmapData ;
    
    
    private function get_bitmapData() : BitmapData
    {
        
        return _texture.bitmapData ;
        
    }
    
    
    private function set_bitmapData( value : BitmapData )
    {
        
        _texture.bitmapData = value ;
        
    }
    
    
    public var viewProjection( get_viewProjection, null ) : Matrix3D ;
    
    
    private function get_viewProjection() : Matrix3D
    {
        
        if( _viewProjectionInvalid )
        {
            
            _viewProjection.copyFrom( inverseSceneTransform ) ;
            _viewProjection.append( _lens.matrix ) ;
            _viewProjectionInvalid = false ;
            
        }
        
        return _viewProjection ;
        
    }
    
    
    override public function dispose( deep : Bool )
    {
        
        super.dispose( deep ) ;
        
    }
    
    
    /**
     * @inheritDoc
     */
    override private function invalidateSceneTransform()
    {
        
        super.invalidateSceneTransform() ;
        _viewProjectionInvalid = true ;
        
    }
    
    
    private function onInvalidateLensMatrix()
    {
        
        _viewProjectionInvalid = true ;
        
    }
    
    
    public var texture( get_texture, null ) : Texture3DProxy ;
    
    
    public function get_texture() : Texture3DProxy
    {
        
        return _texture ;
        
    }
    
    
}
